import numpy as np
from sklearn import preprocessing
from sklearn.svm import SVC
input_file = 'F:/python学习资料/Python-Machine-Learning-Cookbook-master/Chapter03/building_event_binary.txt'

X = []
count = 0
# 读取数据
with open(input_file,'r') as f:
    for line in f.readlines():
        data = line[:-1].split(",")
        X.append([data[0]]+data[2:])

X = np.array(X)


# 把字符串数据转化为数字形式
label_encoder = []
X_encoded = np.empty(X.shape)
for i ,item in enumerate(X[0]):
    if item.isdigit():
        X_encoded[:,i] = X[:,i]
    else:
        label_encoder.append(preprocessing.LabelEncoder())
        X_encoded[:,i] = label_encoder[-1].fit_transform(X[:,i])

X = X_encoded[:,:-1].astype(int)
y = X_encoded[:,-1].astype(int)

# 构建模型
params = {'kernel':'rbf','probability':True,'class_weight':'balanced'}
classifier = SVC(**params,gamma='auto')
classifier.fit(X,y)
# 交叉验证
from sklearn import model_selection
accuracy = model_selection.cross_val_score(classifier,X,y,scoring='accuracy',cv=3)
print("Accuracy of the classifier:"+str(round(100*accuracy.mean(),2))+'%')

# 测量单个数据
input_data = ['Tuesday','12:30:00',"21",'23']
input_data_encoded = [-1] * len(input_data)
count = 0


for i,item in enumerate(input_data):
    if item.isdigit():
        input_data_encoded[i] = int(input_data[i])
    else:
        input_data_encoded[i] = int(label_encoder[count].transform([input_data[i]]))
        count = count+1

input_data_encoded = np.array(input_data_encoded)

# 预测特定数据并打印结果
output_class = classifier.predict([input_data_encoded])
print('Output class:',label_encoder[-1].inverse_transform(output_class)[0])

